package com.pagepub.core.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class SystemCfgHelper {
	private static String SYSTEM_CFG_FILE_NAME = "/config/systemCfg.xml";

	private static Properties systemCfgProps = null;

	private static Properties validatorForwardUrlCfgProps = null;

	private static Map<String, String> validateFormCfgMap = new HashMap<String, String>();
	static {
		initSystemConfig();
		initValidatorForwardUrl();
	}

	/**
	 * 获取systemCfg.xml中Key=resource的配置值(资源文件存放位置)
	 * 
	 * @return
	 */
	public static String getResource() {
		return replaceCR(systemCfgProps.getProperty("resource"));
	}

	/**
	 * 获取systemCfg.xml中Key=defaultResource的配置值(默认的资源文件名称)
	 * 
	 * @return
	 */
	public static String getDefaultResource() {
		return replaceCR(systemCfgProps.getProperty("defaultResource"));
	}

	/**
	 * 取得设置系统session的名称,没有返回""
	 * 
	 * @return
	 */
	public static String getSessionName() {
		return StringUtil.obj2Str(getSystemCfgProps()
				.getProperty("sessionName"));
	}

	/**
	 * 把exp字串中回车换行符替换为""
	 * 
	 * @param exp
	 * @return
	 */
	public static String replaceCR(String exp) {
		if (exp == null) {
			return null;
		}
		String expStr = "[\\t\\r\\n\\v　]";
		String cfgKey = StringUtil.replaceRegEx(exp, expStr, "");
		// String cfgKey = StringUtil.replace(exp, "\n", "");
		// cfgKey = StringUtil.replace(cfgKey, "\r", "");
		// cfgKey = StringUtil.replace(cfgKey, "\t", "");
		// cfgKey = StringUtil.trimChr(cfgKey, " ");
		return cfgKey.trim();
	}

	/**
	 * 获取验证表单提示信息
	 * 
	 * @param propertyName
	 *            验证的字段变量名称

	 * @param vFormClassName
	 *            验证框架配置文件(validation.xml对应的form name)
	 * @return
	 */
	public static String getValidateFormPropertiesMsg(String propertyName,
			String vFormClassName) {
		String isExitV = validateFormCfgMap.get(vFormClassName);
		if (isExitV != null && isExitV.length() > 0) {
			return StringUtil.getLocalResourceStr(propertyName,
					validateFormCfgMap.get(vFormClassName));
		} else {
			VFP vfp = getValidateFormPropertiesName(propertyName);
			validateFormCfgMap.put(vFormClassName, vfp.getCfgFileName());
			return StringUtil.getLocalResourceStr(propertyName,
					validateFormCfgMap.get(vFormClassName));
		}
	}

	/**
	 * 取得表单错误提示信息对应在哪个配置文件中
	 * 
	 * @param vFormClassName
	 * @throws Exception
	 */
	private static VFP getValidateFormPropertiesName(String propertyName) {
		String[] cfgs = replaceCR(systemCfgProps.getProperty("validateFormCfg"))
				.split(",");
		for (int i = 0; i < cfgs.length; i++) {
			cfgs[i] = cfgs[i].substring(cfgs[i].indexOf("/") + 1, cfgs[i]
					.lastIndexOf("."));
			cfgs[i] = cfgs[i].replaceAll("/", ".");
			String str = StringUtil.getLocalResourceStr(propertyName, cfgs[i]);
			if (str.length() > 0) {
				VFP vfp = new VFP();
				vfp.setCfgFileName(cfgs[i]);
				vfp.setResourceStr(str);
				return vfp;
			}
		}
		throw new RuntimeException(propertyName
				+ "没有在validateFormCfg-xxx.properties配置文件中!");

	}

	static private class VFP {
		private String resourceStr;

		private String cfgFileName;

		public String getCfgFileName() {
			return cfgFileName;
		}

		// public String getResoruce()
		// {
		// int start=cfgFileName.indexOf("/")==0?1:0;
		// int
		// end=cfgFileName.lastIndexOf(".")>0?cfgFileName.lastIndexOf("."):cfgFileName.length();
		// return cfgFileName.substring(start,end);
		// }
		public void setCfgFileName(String cfgFileName) {
			this.cfgFileName = cfgFileName;
		}

		public String getResourceStr() {
			return resourceStr;
		}

		public void setResourceStr(String resourceStr) {
			this.resourceStr = resourceStr;
		}
	}

	/**
	 * 获取systemCfg.xml中Key=valitorCfgFileName的配置值

	 * 
	 * @return
	 */
	public static String getValitorCfgFileName() {
		return replaceCR(systemCfgProps.getProperty("valitorCfgFileName"));
	}

	/**
	 * 获取systemCfg.xml中key=webRequestForwardUrlCfg配置的值(验证表单通不过设置的返回URL)
	 * 
	 * @return
	 */
	public static String getValidtorForwardUrlCfgStr() {
		return replaceCR(systemCfgProps.getProperty("forwardUrlCfg"));
	}

	/**
	 * 获取systemCfg.xml中key=springCfgXml配置的值(数据库、hibernate的pojo、service配置文件等)
	 * 
	 * @return
	 */
	public static String getSpringCfgXml() {
		return replaceCR(systemCfgProps.getProperty("springCfgXml"));
	}

	/**
	 * 取得请求限制流量大小(byte),等于-1表示没有设置否则有设置

	 * 
	 * @return
	 */
	public static long getLimitRequestSize() {
		if (StringUtil.obj2Str(
				getSystemCfgProps().getProperty("limitRequestSize")).length() > 0) {
			return 1024 * 1024 * (new Long(getSystemCfgProps().getProperty(
					"limitRequestSize"))).longValue();
		} else {
			return -1;
		}
	}

	/**
	 * 获取systemCfg.xml中key=baseRequestUnit配置的值(初始化权限、会话、表单验证、流量限制等等基础应用类RequestCallActionMethod.class必须配置)
	 * 
	 * @return
	 */
	public static String[] getBaseRequestUnit() {
		String tmp = replaceCR(systemCfgProps.getProperty("baseRequestUnit"));
		if (tmp == null || tmp.length() == 0) {
			return null;
		}
		return tmp.split(",");
	}

	/**
	 * 获取systemCfg.xml的所有配置

	 * 
	 * @return
	 */
	public static Properties getSystemCfgProps() {
		if (systemCfgProps == null || systemCfgProps.size() == 0) {
			initSystemConfig();
		}
		return systemCfgProps;
	}

	/**
	 * 初始化systemCfg.xml数据到缓存中
	 * 
	 */
	private static void initSystemConfig() {
		try {
			systemCfgProps = new Properties();
			systemCfgProps.loadFromXML(SystemCfgHelper.class
					.getResourceAsStream(SYSTEM_CFG_FILE_NAME));
		} catch (Exception e) {
		}
	}

	/**
	 * 取得webRequestForwardUrlCfg.properties对象
	 * 
	 * @return
	 */
	public static Properties getValidatorForwardUrlCfgProps() {
		if (validatorForwardUrlCfgProps == null
				|| validatorForwardUrlCfgProps.size() == 0) {
			initValidatorForwardUrl();
		}
		return validatorForwardUrlCfgProps;
	}

	/**
	 * 初始化validatorForwardUrlCfg.properties文件内容
	 * 
	 */
	private static void initValidatorForwardUrl() {
		try {
			validatorForwardUrlCfgProps = new Properties();
			String[] cfgs = StringUtil.split(replaceCR(getSystemCfgProps()
					.getProperty("forwardUrlCfg")), ",");
			Properties pro = new Properties();
			for (int i = 0; i < cfgs.length; i++) {
				try {
					pro
							.load(SystemCfgHelper.class
									.getResourceAsStream(cfgs[i]));
				} catch (IOException e) {
					continue;
				}
				validatorForwardUrlCfgProps.putAll(pro);
			}

		} catch (Exception e) {
		}
	}

	/**
	 * 获取显示信息的组件forward字串
	 * 
	 * @return
	 */
	public static String getForwardWebMsg() {
		return validatorForwardUrlCfgProps.getProperty("webmsg");
	}

//	/**
//	 * 获取systemCfg.xml中key=JDBCBeanMapCfg配置的值(主要用在系统生成的vo与xml对应,同hibernate的hbm.xml格式相似)
//	 * 
//	 * @return
//	 */
//	public static String[] getJDBCBeanMapCfgStr() {
//		String tmp = replaceCR(systemCfgProps.getProperty("JDBCBeanMapCfg"));
//		if (tmp == null || tmp.length() == 0) {
//			return null;
//		}
//		return tmp.split(",");
//	}
//
//	public static void main(String[] args) {
//		System.out.println(replaceCR("	　dfd	 　").length());
//	}
}
